package com.gopay.common.domain.bank.pab.bcdl;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;

import com.gopay.common.domain.BaseEntity;


/**
 * 深发展银行银企直连消息报文头
 * @author liujy
 * 
 */
public class PABOnlineHeaderMessage extends BaseEntity {

	private static final long serialVersionUID = 7455829859459505047L;
	/** 报文头 */
	private String msgType;// 报文类别编号，固定填填A00101
	private String msgCharset;// 报文编码，01-GBK
	private String protocol;// 通讯协议，01:tcpip
	private String merchantCode; // 企业银企直连标准代码
	private String msgLength;// 报文长度
	private String txnType;// 交易码
	private String userOpid;// 操作员代码
	private String serviceFlag;// 服务类型 01-请求  02-应答
	private String orderDate;// 交易日期
	private String orderTime;// 交易时间
	private String orderId;// 请求方系统流水号（唯一标识一笔交易，如果某种交易要有多次请求的才能完成的，多个交易请求包流水号要保持一致）
	private String retCd;// 返回码
	private String remark;// 返回描述
	private String repeatFlag;// 后续包标志 0-结束 1-还有后续包
	private String reqCount;// 请求次数
	private String signFlag;// 签名标识 0-不签名 1-签名 （填0，企业不管，由银行客户端完成）
	private String signFormat;// 签名数据包格式  0-裸签 1-PKCS7（填1，企业不管，由银行客户端完成）
	private String signAlgorithm;// 签名算法  RSA-SHA1
	private String signLength;// 签名长度  0000000000-企业不需要签名
	private String fileCount;// 附件数目

	/** 签名 */
	private String sign;// 签名

	/**
	 * 报文头
	 * 
	 * @return
	 */
	public String getHeaderStr() {
		StringBuffer b = new StringBuffer();
		b.append(formatDate(msgType, 6));
		b.append(formatDate(msgCharset, 2));
		b.append(formatDate(protocol, 2));
		b.append(formatDate(merchantCode, 20));
		b.append(formatDate(msgLength, 10));
		b.append(formatDate(txnType, 6));
		b.append(formatDate(userOpid, 5));
		b.append(formatDate(serviceFlag, 2));
		b.append(formatDate(orderDate, 8));
		b.append(formatDate(orderTime, 6));
		b.append(formatDate(orderId, 20));
		b.append(formatDate(retCd, 6));
		b.append(formatDate(remark, 100));
		b.append(formatDate(repeatFlag, 1));
		b.append(formatDate(reqCount, 3));
		b.append(formatDate(signFlag, 1));
		b.append(formatDate(signFormat, 1));
		b.append(formatDate(signAlgorithm, 12));
		b.append(formatDate(signLength, 10));
		b.append(formatDate(fileCount, 1));
		return b.toString();
	}
	
	public String formatDate(String str, int length) {
		return StringUtils.rightPad(str, length, " ");
	}

	/**
	 * 报文体长度
	 * 
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public String getBodyLength(String bodyXml) throws UnsupportedEncodingException {
		try {
			return String.valueOf(bodyXml.getBytes("GBK").length);
		} catch (UnsupportedEncodingException e) {
			throw e;
		}
	}

	public String getMsgType() {
		return msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	public String getMsgCharset() {
		return msgCharset;
	}

	public void setMsgCharset(String msgCharset) {
		this.msgCharset = msgCharset;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getMsgLength() {
		return msgLength;
	}

	public void setMsgLength(String msgLength) {
		this.msgLength = msgLength;
	}

	public String getTxnType() {
		return txnType;
	}

	public void setTxnType(String txnType) {
		this.txnType = txnType;
	}

	public String getUserOpid() {
		return userOpid;
	}

	public void setUserOpid(String userOpid) {
		this.userOpid = userOpid;
	}

	public String getServiceFlag() {
		return serviceFlag;
	}

	public void setServiceFlag(String serviceFlag) {
		this.serviceFlag = serviceFlag;
	}

	public String getOrderDate() {
		return orderDate;
	}

	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}

	public String getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(String orderTime) {
		this.orderTime = orderTime;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getRetCd() {
		return retCd;
	}

	public void setRetCd(String retCd) {
		this.retCd = retCd;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getRepeatFlag() {
		return repeatFlag;
	}

	public void setRepeatFlag(String repeatFlag) {
		this.repeatFlag = repeatFlag;
	}

	public String getReqCount() {
		return reqCount;
	}

	public void setReqCount(String reqCount) {
		this.reqCount = reqCount;
	}

	public String getSignFlag() {
		return signFlag;
	}

	public void setSignFlag(String signFlag) {
		this.signFlag = signFlag;
	}

	public String getSignFormat() {
		return signFormat;
	}

	public void setSignFormat(String signFormat) {
		this.signFormat = signFormat;
	}

	public String getSignAlgorithm() {
		return signAlgorithm;
	}

	public void setSignAlgorithm(String signAlgorithm) {
		this.signAlgorithm = signAlgorithm;
	}

	public String getSignLength() {
		return signLength;
	}

	public void setSignLength(String signLength) {
		this.signLength = signLength;
	}

	public String getFileCount() {
		return fileCount;
	}

	public void setFileCount(String fileCount) {
		this.fileCount = fileCount;
	}

	public String getSign() {
		if (StringUtils.isNotEmpty(sign)) {
			return sign.trim();
		}
		return "";
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
}
